"""
https://www.tensorflow.org/guide/autodiff
"""

import sys
from python_ai.common.xcommon import *
import tensorflow as tf

print(tf.__version__)
tf.random.set_seed(777)

sep('y = x**2')
x = tf.Variable(3.0, name='x')
a = tf.constant(2.0, name='a')
with tf.GradientTape() as tape:
    y = x ** a
sep('watched vars')
print([(v.name, v.value()) for v in tape.watched_variables()])
sep('dy_dx, dy_da')
dy_dx, dy_da = tape.gradient(y, [x, a])
print(dy_dx)
print(dy_da)

sep('dy_dx, dy_da; after add watch to a')
with tf.GradientTape() as tape:
    y = x ** a
    tape.watch(a)
dy_dx, dy_da = tape.gradient(y, [x, a])
print(dy_dx)
print(dy_da)
